package com.binarycube.webserver.session;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.UUID;

import com.binarycube.webserver.wrappers.WrappedRequest;

public class Session  {
	
	public static final String REDIRECT = "session.redirect";
	public static final String CREATED = "session.created";
	public static final String USERNAME = "session.username";
	public static final String DOMAIN = "session.domain";
	public static final String IP = "session.ip";
	
	
	String _id;
	boolean _authenticated;
	boolean _active;
	private Map<String, Object> _properties;
	
	

	private Session(){
		_properties = new HashMap<String,Object>();
		_id = UUID.randomUUID().toString();
		_properties.put(CREATED, new Date());
		_active = true;
		_authenticated = false;
	}
	


	public Session(WrappedRequest request) {
		this();
		_properties.put(DOMAIN, request.getDomain());
		_properties.put(IP, request.getIP());
	}



	public void setAuthenticated() {
		if (!_active) return;
		_authenticated = true;
	}
	
	
	public String getID(){
		return _id;
	}
	
	public void setID(String id) {
		this._id = id;
	}
	
	public Date getCreated() {
		if (!_active) return null;
		return (Date)_properties.get(CREATED);
	}


	public Object getProperty(String key) {
		if (!_active) return null;
		return _properties.get(key);
	}
	
	public void setProperty(String key, Object value) {
		if (!_active) return;
		if (!key.equals(CREATED))  _properties.put(key, value);
	}
	
	
	public boolean isActive(){
		return _active;
	}
	
	public void deActivate(){
		_active = false;
	}


	public boolean isAuthenicated() {
		return _authenticated;
	}



	public void setUID(String uid) {
		this.setProperty(USERNAME, uid);
	}
	
	public String getUID(){
		return (String)this.getProperty(Session.USERNAME);
	}



	public Set<String> getPropertyKeys() {
		return _properties.keySet();
	}
	
}
